//
//  ZH_MineSetUpViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/24.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MineSetUpViewController.h"
#import "ZH_UserPersonInforTableViewCell.h"
#import "ZH_WeiXinViewController.h"
#import "ZH_QQViewController.h"
#import "ZH_MineAddressViewController.h"
#import "ZH_MineViewModel.h"
#import "WSModuleManager.h"
@interface ZH_MineSetUpViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)UIButton *button;
@property (nonatomic,strong)ZH_MineViewModel *viewModel;
@end

@implementation ZH_MineSetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self initData];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initData{
    WEAKSelf;
    [self.viewModel showOptionResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            if (![data[@"data"] isKindOfClass:[NSNull class]]) {
                weakSelf.dic = data[@"data"];
            }
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)initUserInterface{
    self.navigationItem.title = @"系统设置";
    self.view.backgroundColor = kFontColorWhite;
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(FITSCALE(50));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.button.mas_top);
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"手机号码";
        [cell updataUI:indexPath.row dic:self.dic];
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 1) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"微信";
        [cell updataUI:indexPath.row dic:self.dic];
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 2) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"QQ";
        [cell updataUI:indexPath.row dic:self.dic];
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 3) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"收货地址";
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 4) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"修改密码";
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 5) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"清除缓存";
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:NO];
        return cell;
    }
    else if (indexPath.row == 6) {
        ZH_UserPersonInforTableViewCell *cell = [ZH_UserPersonInforTableViewCell cellWithTableView:tableView];
        cell.titleString = @"消息提醒";
        [cell setHiddenRightLabel:NO];
        [cell setHiddenRightImage:YES];
        [cell SiwtchHidden:NO];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1) {
        ZH_WeiXinViewController *vc = [[ZH_WeiXinViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 2){
        ZH_QQViewController *vc = [[ZH_QQViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if(indexPath.row == 3){//地址管理
        ZH_MineAddressViewController *vc = [[ZH_MineAddressViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)buttons{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定退出登录?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    // cancle
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL];
    // sure
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [TFUserDefault removeAll];
        [WSModuleManager showLoginMoudle];
    }];
    [alertController addAction:cancleAction];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];

}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kFontColorWhite;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.scrollEnabled = NO;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (UIButton *)button{
    if (!_button) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"退出登录" target:self action:@selector(buttons)];
        [button setTitleColor:[UIColor colorWithHexString:@"#00C9AE"] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor colorWithHexString:@"#CEECE8"];
        button.titleLabel.font = kFontSize_34;
        [self.view addSubview:_button = button];
    }
    return _button;
}
- (ZH_MineViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MineViewModel alloc] init];
    }
    return _viewModel;
}
@end
